package cn.abellflower.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * @link https://www.throwable.club/2020/08/04/java-concurrency-thread-state/
 * @Description
 * @Author gbzdyf@126.com
 * @Date 2020/10/9 11:29 下午
 */
public class ThreadState {

    public static void main(String[] args) throws Exception {
        //线程实例未启动状态
        Thread thread = new Thread();
        System.out.println(thread.getState());


        //可运行下线程状态 当Java线程实例调用了Thread#start()就会进入RUNNABLE(包含 READY & RUNNABLE)
        //READY 该线程可以被线程调度器进行调度变更为RUNNABLE
        //RUNNABLE 该线程正在运行
        //当Java线程实例调用Thread#yield()方法被调用时由于线程调度器的调度，线程实例的状态有可能由RUNNABLE变为READY
        Thread threadRun = new Thread(() -> {
            while (true) {
                Thread.yield();
            }
        });
        threadRun.start();
        Thread.sleep(2000);
        System.out.println(threadRun.getState());

        //WAITING
        Thread threadWait = new Thread(() -> {
            LockSupport.park();
            while (true) {
                Thread.yield();
            }
        });
        threadWait.start();
        Thread.sleep(50);
        System.out.println(threadWait.getState());
        LockSupport.unpark(threadWait);
        Thread.sleep(50);
        System.out.println(threadWait.getState());
    }
}
